package com.app.megaprosoft.DB;

import java.sql.*;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class ArGroup {
	
	 
	 ResultSet rs = null;

	 SQLiteDBClass myDb;
	 SQLiteDatabase db;
	 Context context;
	 private static final String TABLE_ArGroup = "_ArGroup";
	 
	public ResultSet selectArGroup(String ArGroupDesc) throws Exception {
		Connect con = new Connect(); 
		String StrSql   = "SELECT	 [OID]," +
									"[LastModified]," +
									"[ModifyDate]," +
									"[ModifyTime]," +
									"[ModifiedBy]," +
									"[UserModify]," +
									"[CreatedDate]," +
									"[CreateDate]," +
									"[CreateTime]," +
									"[CreatedBy]," +
									"[UserCreate]," +
									"[IsActive]," +
									"[IsDeleted]," +
									
									"[ArGroupCode],"+
									"[ArGroupDesc],"+
									"[ArGroupMainCode],"+
									"[ArGroupMainDesc]," +
									"[ArGroupMainDiscount] " +
                			 
                		  "FROM		 [dbo].[udfArGroup] ()" +
                		  
                		  "WHERE	 [IsDeleted]='0' "; 
		
		if (ArGroupDesc != null && !ArGroupDesc.equals("")) {
			StrSql += "AND [ArGroupDesc] LIKE '%" + ArGroupDesc + "%' ";
		}
		
		return con.selectData(StrSql);
//    	return sqlsel.getReSultSQL(StrSql);
	 }
	
//	 public void closeConnecttion() throws Exception {
//    	 if (conn != null) {
//             conn.close();
//         }
//    }
	 
	 public void DEFINE_ArGroup() {
			rs = null;
			
			myDb = new SQLiteDBClass(getContext());
			db = myDb.getWritableDatabase();

			try {
				rs = selectArGroup("");
				while (rs.next()) {
					ContentValues Val = new ContentValues();
					Val.put("ArGroupOID", rs.getString("OID"));
					Val.put("ArGroupCode", rs.getString("ArGroupCode"));
					Val.put("ArGroupDesc", rs.getString("ArGroupDesc"));
					Val.put("ArGroupMainCode", rs.getString("ArGroupMainCode"));
					Val.put("ArGroupMainDesc", rs.getString("ArGroupMainDesc"));
					Val.put("ArGroupMainDiscount", rs.getString("ArGroupMainDiscount"));
					db.insert(TABLE_ArGroup, null, Val);
				}
				rs.close();
			} catch (Exception e) {
				Log.d("TABLE_ArGroup", "Create Table not Successfully.");
			}
			
			Log.d("TABLE_ArGroup", "Create Table Successfully.");
			db.close();
	}

	 public String[][] SelectAll_ArGroup(String StrArGroupMainOID) throws Exception{
			// TODO Auto-generated method stub
			try {
				String arrData[][] = null;
				myDb = new SQLiteDBClass(getContext());
				db = myDb.getWritableDatabase();
				String strSQL = "SELECT  ArGroupOID As _id , * FROM "
						+ TABLE_ArGroup;
				if (!StrArGroupMainOID.equals("")) {
					strSQL += " WHERE [ArGroupMainCode] LIKE '" + StrArGroupMainOID
							+ "' ";
				}
				Cursor cursor = db.rawQuery(strSQL, null);
				if (cursor != null) {
					if (cursor.moveToFirst()) {
						arrData = new String[cursor.getCount()][cursor
								.getColumnCount()];
						int i = 0;
						do {
							arrData[i][0] = cursor.getString(1);
							arrData[i][1] = cursor.getString(2);
							arrData[i][2] = cursor.getString(3);
							arrData[i][3] = cursor.getString(4);
							arrData[i][4] = cursor.getString(5);
							arrData[i][5] = cursor.getString(6);
							i++;
						} while (cursor.moveToNext());
					}
				}
				cursor.close();
				return arrData;
			} catch (Exception e) {
				return null;
			}
		}
	 
	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}
	 
	 
}
